package com.futures.questionbank.main;

import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.futures.questionbank.Constants.MessageEvent;
import com.futures.questionbank.R;
import com.futures.questionbank.Utils.UItils;
import com.futures.questionbank.adapter.GoodsAdapter;
import com.futures.questionbank.adapter.TabFragmentPagerAdapter;
import com.futures.questionbank.base.BaseActivity;
import com.futures.questionbank.db.Constants;
import com.futures.questionbank.db.table.option;
import com.futures.questionbank.db.table.problem;
import com.futures.questionbank.db.table.problem_Table;
import com.futures.questionbank.fragment.Fragment_Control;
import com.futures.questionbank.view.CommonDialog;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.sql.language.OperatorGroup;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.BaseModel;
import com.raizlabs.android.dbflow.structure.database.transaction.ProcessModelTransaction;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 试题
 */
public class ExamActivity extends BaseActivity {

    ViewPager viewPager;
    @BindView(R.id.ib_back)
    ImageButton ibBack;
    @BindView(R.id.iv_ti)
    ImageView ivTi;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;
    public  GoodsAdapter mAdapter;
    private int exam_id;
    private int i;


    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.exam_activity);
        setHead_title(View.GONE);
        // setTitle("做题");
        ibBack.setOnClickListener(view -> getSubmit());
    }

    @Override
    protected void loadData() {
        initView();
    }




    private void initView() {
        UItils.showDialog(this);
        Constants.maps = new HashMap<>();
        viewPager = findViewById(R.id.viewPager);
        exam_id = getIntent().getIntExtra("index", 0);
        List<problem> problems = SQLite.select().
                from(problem.class).where(problem_Table.exam_id.eq(exam_id)).queryList();

        //  problem problemModel = SQLite.select().from(problem.class).where(problem_Table.exam_id.eq(exam_id)).querySingle();
        // List<option> options = SQLite.select().
        // from(option.class).where(option_Table.problem_id.eq(problemModel.getId())).queryList();

        List<Fragment> list = new ArrayList<>();
        for (int i = 0; i < problems.size(); i++) {
            Fragment_Control fragment = new Fragment_Control(i,problems,viewPager);
            list.add(i,fragment);
        }

        //  list.add(new Fragment_Control() );
        TabFragmentPagerAdapter adapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), list);
        viewPager.setAdapter(adapter);

      //  mAdapter = new GoodsAdapter(ExamActivity.this, problems, new ArrayList<option>(), viewPager);
       // viewPager.setAdapter(mAdapter);
    }



    @OnClick({R.id.ib_back, R.id.iv_ti, R.id.tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ib_back:
                finish();
                break;
            case R.id.iv_ti:

                break;
            case R.id.tv_submit:
                getSubmit();

                break;
                default:
        }
    }

    private void getSubmit() {
        String s = "确定交卷吗?";
        new CommonDialog(this, R.style.dialog, s, (dialog, confirm) -> {
            if (confirm) {
                //批量插入
                //同步事务
                int i = 0;
                for(Map.Entry<Integer, String> entry : Constants.maps.entrySet()){
                    int mapKey = entry.getKey();
                    String mapValue = entry.getValue();
                    OperatorGroup op = OperatorGroup.clause(OperatorGroup.clause()
                            .and(problem_Table.exam_id.eq(exam_id))
                            .and(problem_Table.index.eq(mapKey)));
                    SQLite.update(problem.class)
                            .set(problem_Table.my_answer.eq(mapValue))
                            .where(op)
                            .execute();
                    i++;
                }
                if (i == Constants.maps.size()){
                    MessageEvent messageEvent = new MessageEvent();
                    messageEvent.setMessageKey(Constants.FAMILY);
                    messageEvent.setMessageValue(exam_id);
                    EventBus.getDefault().post(messageEvent);
                    finish();
                }
               //
            }
            dialog.dismiss();
        }).show();
    }
}
